<template>
  <div class="directorys-container">
      <el-card>
      <!-- 面包屑 -->
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
             <!-- 筛选 -->
        <el-row>
          <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
                <el-form-item label="目录名称">
                  <el-input style="200px" v-model="requestParams.directoryName"></el-input>
                  </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="requestParams.state">
                      <el-option :value="1" label="启用"></el-option>
                      <el-option :value="0" label="禁用"></el-option>
                    </el-select>
                  </el-form-item>
                <el-form-item style="text-align:right">
                  <el-button @click="clear()">清除</el-button>
                  <el-button @click="filter()" type="primary">搜索</el-button>
                </el-form-item>
          </el-form>
          </el-col>
          <el-col :span="6" style="text-align:right">
              <el-button v-if="subject.id && subject.name" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
              <el-button icon="el-icon-edit" @click="openDialog()" type="success" size="small">新增目录</el-button>
          </el-col>
        </el-row>
        <!-- 数据记录 -->
        <el-alert
          style="margin-bottom:15px"
          :title="alertText"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- 数据表格 -->
        <el-table :data="dataList">
            <el-table-column prop="id" label="序号" type="index" width="80px"></el-table-column>
              <el-table-column  prop="subjectName" label="所属学科"></el-table-column>
              <el-table-column  prop="directoryName" label="目录名称">
              </el-table-column>
              <el-table-column prop="username"  label="创建者">
              </el-table-column>
              <el-table-column label="创建日期">
          <template
          slot-scope="scope">
          <span>
          {{scope.row.addDate|parseTimeByString}}
          </span>
          </template>
          </el-table-column>
              <el-table-column prop="totals"  label="面试题数量">
              </el-table-column>
              <el-table-column  prop="state" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state==1">启用</span>
            <span v-else>禁用</span>
          </template>
              </el-table-column>

          <!-- 操作按钮 -->
          <el-table-column
            align="center"
            label="操作"
            width="150px">
            <template  slot-scope="scope">
              <el-button
              @click="toggleState(scope.row)"
              type="text"
              >
                <span v-if="scope.row.state==0">启用</span>
                <span v-else>禁用</span>
              </el-button>
            <el-button
            type="text"
            @click="openDialog(scope.row)"
            :disabled="scope.row.state===1"
            >修改</el-button>
            <el-button
            type="text"
            @click="delDirectory(scope.row)"
            :disabled="scope.row.state===1||scope.row.totals > 0"
            >删除</el-button>
            </template>
           </el-table-column>
        </el-table>
        <!-- 分页 -->
            <el-pagination
              style="margin-top:20px;text-align:right"
              background
              :total="total"
              :page-size="requestParams.pagesize"
              :current-page="requestParams.page"
              :page-sizes="[5,10,20,50]"
              hide-on-single-page
              @current-change="pager"
              @size-change="handleSizeChange"
              layout="prev, pager, next, sizes, jumper"
            ></el-pagination>
        <!-- 新增标签弹层 -->
      </el-card>

    <directorys-add ref="directorysAdd" :data="currDirectory" @updateList="updateList()"></directorys-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import DirectorysAdd from '../components/directorys-add'
export default {
  name: 'CompanysList',
  components: { DirectorysAdd },
  data () {
    return {
      dataList: [], // 列表数据
      total: null,
      alertText: '',
      requestParams: {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      currDirectory: {}
    }
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  // 调用请求
  created () {
    this.getList()
  },
  watch: {
    '$route.query': function () {
      this.getList()
    }
  },
  methods: {
    openDialog (directory = {}) {
      this.currDirectory = directory
      this.$nextTick(() => {
        this.$refs.directorysAdd.open()
      })
    },
    // 切换状态
    async toggleState (directory) {
      await changeState({
        id: directory.id,
        state: directory.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      directory.state = directory.state === 1 ? 0 : 1
    },
    async delDirectory (directory) {
      await this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(directory)
      this.$message.success('删除成功')
      this.getList()
    },
    // 清除筛选条件
    clear () {
      this.requestParams = {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 进行筛选
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    // 获取列表数据
    async getList () {
      this.requestParams.subjectID = this.subject.id || null
      const { data: res } = await list(this.requestParams)
      this.dataList = res.items
      // 条数
      this.total = res.counts
      this.alertText = `共 ${this.total} 条记录`
    },
    // 切换条数
    handleSizeChange (size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    // 切换分页
    pager (np) {
      this.requestParams.page = np
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currDirectory.id) {
        this.requestParams.page = 1
      }
      this.getList()
    }
  }
  // 组件更新
}
</script>
<style lang="scss" scoped>
.directorys-container{
  padding: 10px;
}
</style>
